package com.ruoyi.cm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.cm.domain.bo.CmBsContractInfoSceneBo;
import com.ruoyi.cm.domain.vo.CmBsContractInfoSceneVo;
import com.ruoyi.cm.domain.CmBsContractInfoScene;
import com.ruoyi.cm.mapper.CmBsContractInfoSceneMapper;
import com.ruoyi.cm.service.ICmBsContractInfoSceneService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 合同建设场景Service业务层处理
 *
 * @author zxs
 * @date 2025-03-11
 */
@RequiredArgsConstructor
@Service
public class CmBsContractInfoSceneServiceImpl implements ICmBsContractInfoSceneService {

    private final CmBsContractInfoSceneMapper baseMapper;

    /**
     * 查询合同建设场景
     */
    @Override
    public CmBsContractInfoSceneVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询合同建设场景列表
     */
    @Override
    public TableDataInfo<CmBsContractInfoSceneVo> queryPageList(CmBsContractInfoSceneBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CmBsContractInfoScene> lqw = buildQueryWrapper(bo);
        Page<CmBsContractInfoSceneVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询合同建设场景列表
     */
    @Override
    public List<CmBsContractInfoSceneVo> queryList(CmBsContractInfoSceneBo bo) {
        LambdaQueryWrapper<CmBsContractInfoScene> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CmBsContractInfoScene> buildQueryWrapper(CmBsContractInfoSceneBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CmBsContractInfoScene> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getContractId() != null, CmBsContractInfoScene::getContractId, bo.getContractId());
        lqw.like(StringUtils.isNotBlank(bo.getSceneName()), CmBsContractInfoScene::getSceneName, bo.getSceneName());
        lqw.eq(bo.getCapacity() != null, CmBsContractInfoScene::getCapacity, bo.getCapacity());
        lqw.eq(bo.getPrice() != null, CmBsContractInfoScene::getPrice, bo.getPrice());
        lqw.eq(bo.getChgPrice() != null, CmBsContractInfoScene::getChgPrice, bo.getChgPrice());
        lqw.eq(bo.getChgedPrice() != null, CmBsContractInfoScene::getChgedPrice, bo.getChgedPrice());
        return lqw;
    }

    /**
     * 新增合同建设场景
     */
    @Override
    public Boolean insertByBo(CmBsContractInfoSceneBo bo) {
        CmBsContractInfoScene add = BeanUtil.toBean(bo, CmBsContractInfoScene.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改合同建设场景
     */
    @Override
    public Boolean updateByBo(CmBsContractInfoSceneBo bo) {
        CmBsContractInfoScene update = BeanUtil.toBean(bo, CmBsContractInfoScene.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CmBsContractInfoScene entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除合同建设场景
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
